using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using TONLI.TJOA.Common;
using TONLI.TJOA.Domain;
using ComponentArt.Web.UI;
using TONLI.Domain.RightManager;
namespace TONLI.TJOA.Anquan {
    public partial class AQK_XXJKHBZ : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack && !FolderGrid.IsCallback) {
                StrWhere = null;
                
                AjaxPro.Utility.RegisterTypeForAjax(typeof(AQK_XXJKHBZ));
                
                if (Session["user"] != null) {
                    Smmuser user = Session["user"] as Smmuser;
                    if (user.CorpDpt.StartsWith("002")) {
                        StrWhere = " dwmc='" + user.Dwmc + "' order by index_ ";
                    } else {
                        treecontent1.Width = 150;
                        buildTree();
                    }                    
                }
                
                BuildFolderGrid();
                FolderGrid.DataBind();
            }
            //FolderGrid.FilterCommand += new Grid.FilterCommandEventHandler(FolderGrid_FilterCommand);
        }

        public void FolderGrid_FilterCommand(object sender, GridFilterCommandEventArgs e) {

            FolderGrid.Filter = e.FilterExpression;
        }

        #region webMethod
        [AjaxPro.AjaxMethod]
        public TJOA_AQK_XXJKHBZ GetNewWF_HD() {
            return new TJOA_AQK_XXJKHBZ();
        }
        [AjaxPro.AjaxMethod]
        public string Getdwmc() {
            string dwmc = "";
            if (Session["user"] != null) {
                Smmuser user = Session["user"] as Smmuser;
                if (user.CorpDpt.StartsWith("002")) dwmc = user.Dwmc;
            }
            return dwmc;
        }
        [AjaxPro.AjaxMethod]
        public int AddWF_HD(TJOA_AQK_XXJKHBZ obj) {
            if (obj.DWMC.Trim() == "") return 0;
            if (obj.USERNAME.Trim() == "") return 0;
            DataService.BaseService().Create<TJOA_AQK_XXJKHBZ>(obj);
            return 1;
        }
        [AjaxPro.AjaxMethod]
        public int DelWF_HD(string id) {
            TJOA_AQK_XXJKHBZ obj = new TJOA_AQK_XXJKHBZ();
            obj.SUID = id;
            DataService.BaseService().Delete<TJOA_AQK_XXJKHBZ>(obj);
            return 1;
        }
        [AjaxPro.AjaxMethod]
        public TJOA_AQK_XXJKHBZ GetWF_HD(object obj) {
            TJOA_AQK_XXJKHBZ ret = DataService.BaseService().GetOneByKey<TJOA_AQK_XXJKHBZ>(obj);
            return ret;
        }
        [AjaxPro.AjaxMethod]
        public int UpdateWF_HD(TJOA_AQK_XXJKHBZ obj) {
            if (obj.DWMC.Trim() == "") return 0;
            if (obj.USERNAME.Trim() == "") return 0;
            DataService.BaseService().Update<TJOA_AQK_XXJKHBZ>(obj);
            return 1;
        }
        #endregion
        private void buildTree() {
            treeview1.Nodes.Clear();
            TreeViewNode node1 = null;
            Hashtable hs =new Hashtable();
            hs.Add("p1", "distinct(DWMC)");
            hs.Add("p2","TJOA_AQK_XXJKHBZ order by DWMC");
            IList list= DataService.BaseService().GetList("SelectTJOA_AQK_XXJKHBZ_dwmcList", hs);
            foreach (TJOA_AQK_XXJKHBZ obj in list) {
                node1 = new TreeViewNode();
                node1.ID = obj.DWMC;
                node1.Text = obj.DWMC;
                node1.ImageUrl = "file.gif";
                treeview1.Nodes.Add(node1);
            }
        }
        private void BuildFolderGrid() {
            IList list =DataService.BaseService().GetList("SelectTJOA_AQK_XXJKHBZListByWhere", StrWhere);
            DataTable dt =TONLI.Common.DataConverter.ToDataTable(list,typeof(TONLI.TJOA.Domain.TJOA_AQK_XXJKHBZ));
            FolderGrid.DataSource = dt;
        }
        public string StrWhere {
            get { return Session["TJOA_AQK_XXJKHBZ_Parentid"] == null ? "1=1 order by index_" : Session["TJOA_AQK_XXJKHBZ_Parentid"].ToString(); }
            set { Session["TJOA_AQK_XXJKHBZ_Parentid"] = value; }
        }
        bool flage1 = false;
        private int curPage {
            get { return Session["TJOA_AQK_XXJKHBZ_page"] == null ? 0 : (int)Session["TJOA_AQK_XXJKHBZ_page"]; }
            set { Session["TJOA_AQK_XXJKHBZ_page"] = value; }
        }
        protected void FolderGrid_NeedRebind(object sender, EventArgs e) {
            FolderGrid.DataBind();
            FolderGrid.CurrentPageIndex = curPage;
        }

        protected void FolderGrid_NeedDataSource(object sender, EventArgs e) {
            BuildFolderGrid();
        }

        protected void FolderGrid_PageIndexChanged(object sender, ComponentArt.Web.UI.GridPageIndexChangedEventArgs e) {
            FolderGrid.CurrentPageIndex = e.NewIndex;
            
            curPage = e.NewIndex;
        }

        protected void FolderGrid_SortCommand(object sender, ComponentArt.Web.UI.GridSortCommandEventArgs e) {
            FolderGrid.Sort = e.SortExpression;
        }

        public void FolderCallBack_Callback(object sender, ComponentArt.Web.UI.CallBackEventArgs args) {
            if (!string.IsNullOrEmpty(args.Parameter)) {
                StrWhere =  " dwmc='" + args.Parameter +"' order by index_ "; 
                BuildFolderGrid();
            }
            FolderGrid.DataBind();
            
            FolderGrid.RenderControl(args.Output);
        }
    }
}